package com.swing.trial;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import net.sourceforge.jdatepicker.DateModel;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.SqlDateModel;

import com.workoutdiary.dao.MCycleRecordDao;
import com.workoutdiary.entities.MCycleRecord;

public class AddMCycleRecordFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldFase;
	private JTextField textFieldDescription;
	private JLabel lblFase;
	private JLabel lblDescription;
	private JButton btnAddRecord;
	private JButton btnCancel;
	private JDatePickerImpl picker;
	
	private TableListener tableListener;
	
	public void setTableListener(TableListener tableListener) {
		this.tableListener = tableListener;
	}
	/**
	 * Create the frame.
	 */
	public AddMCycleRecordFrame() {
		setTitle("Add MCycle Record");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][74.00,grow][267.00,grow]",
				"[][38.00][41.00,grow][45.00,grow][54.00]"));

		JDatePanelImpl panel = new JDatePanelImpl(new SqlDateModel());
		picker = new JDatePickerImpl(panel);

		contentPane.add(picker, "cell 2 1,growx");

		lblFase = new JLabel("Fase");
		contentPane.add(lblFase, "cell 1 2,alignx trailing");

		textFieldFase = new JTextField();
		contentPane.add(textFieldFase, "cell 2 2,growx");
		textFieldFase.setColumns(10);

		lblDescription = new JLabel("Description");
		contentPane.add(lblDescription, "cell 1 3,alignx trailing");

		textFieldDescription = new JTextField();
		contentPane.add(textFieldDescription, "cell 2 3,growx");
		textFieldDescription.setColumns(10);

		btnAddRecord = new JButton("Add");

		contentPane.add(btnAddRecord, "flowx,cell 2 4");

		btnCancel = new JButton("Cancel");
		contentPane.add(btnCancel, "cell 2 4");

		setListeners();
	}

	private void setListeners() {
		btnAddRecord.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// TODO add validation
				addMCycleRecord();
				//TODO check if added
				boolean res = true;
				if(res) {
					tableListener.refreshTable();
					setVisible(false);
				}
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
	}

	private void addMCycleRecord() {
		DateModel<SqlDateModel> model = (DateModel<SqlDateModel>) picker
				.getModel();

		int userId = 2;
		long timestamp = getTimestamp(model.getDay(), model.getMonth(),
				model.getYear());

		// TODO fases from db table
		int fase = Integer.parseInt(textFieldFase.getText());
		String description = textFieldDescription.getText();

		MCycleRecord record = new MCycleRecord(userId, timestamp, fase,
				description);
		MCycleRecordDao mCycleRecordDao = new MCycleRecordDao();
		mCycleRecordDao.create(record);

		System.out.println("created " + record);
	}

	private long getTimestamp(int day, int month, int year) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.set(year, month, day);
		return cal.getTimeInMillis();
	}

}
